package classes;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

public class QueryBD {

	public ArrayList<DVD> list;

	public QueryBD() {
		list = new ArrayList<DVD>();
		load();
		remplirCatalogue();
	}

	public boolean addDvd(DVD d) {
		return list.add(d);
	}

	public boolean removeDvd(DVD d) {
		return list.remove(d);
	}

	private void load() {
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
		} catch (Exception e) {
			System.err.println("Erreur lors du chargement du driver");
			System.exit(1);
		}
	}

	public void remplirCatalogue() {
		int id;
		String desc;
		double p;
		try {
			Connection cx = DriverManager.getConnection(
					"jdbc:oracle:thin:@miage03.dmiage.u-paris10.fr:1521:MIAGE",
					"maletell", "matthieu");
			Statement state = cx.createStatement();
			ResultSet rs = state.executeQuery("SELECT * FROM CATALOGUE");
			while (rs.next()) {
				id = rs.getInt("ID_DVD");
				desc = rs.getString("DESCRIPTION");
				p = rs.getDouble("PRIX");
				addDvd(new DVD(id, desc, p));
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public ArrayList<DVD> getList() {
		return list;
	}

}
